﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.WebApiTool
{
    public class XDoc
    {
        private XmlDocument _xDoc;

        public XDoc(string file)
        {
            if(File.Exists(file))
            {
                try
                {
                    _xDoc = new XmlDocument();
                    _xDoc.Load(file);
                }
                catch(Exception ex)
                {
                    _xDoc = null;
                }
            }
        }

        public string GetSummary4T(Type type)
        {
            if (_xDoc == null)
            {
                return null;
            }

            string xName = string.Format("T:{0}", type.FullName);
            XmlNode xNode = _xDoc.SelectSingleNode(string.Format("./doc/members/member[@name='{0}']/summary", xName));
            if(xNode!=null)
            {
                return xNode.InnerText.Trim();
            }
            return string.Empty;
        }

        public string GetSummary4P(PropertyInfo prop)
        {
            if (_xDoc == null)
            {
                return null;
            }

            string xName = string.Format("P:{0}.{1}", prop.DeclaringType.FullName, prop.Name);
            XmlNode xNode = _xDoc.SelectSingleNode(string.Format("./doc/members/member[@name='{0}']/summary", xName));
            if (xNode != null)
            {
                return xNode.InnerText.Trim();
            }
            return string.Empty;
        }

        public string GetSummary4F(FieldInfo field)
        {
            if (_xDoc == null)
            {
                return null;
            }

            string xName = string.Format("F:{0}.{1}", field.DeclaringType.FullName, field.Name);
            XmlNode xNode = _xDoc.SelectSingleNode(string.Format("./doc/members/member[@name='{0}']/summary", xName));
            if (xNode != null)
            {
                return xNode.InnerText.Trim();
            }
            return string.Empty;
        }
    }
}
